//
//  CCWelcomeVC.swift
//  swift_weibo
//
//  Created by ccl on 16/3/4.
//  Copyright © 2016年 LittleBoy. All rights reserved.
//

import UIKit
import SDWebImage
class CCWelcomeVC: UIViewController {
    /// 图片对下约束
    var imageBottomConstrain: NSLayoutConstraint?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        creatUI();
        
        if let avatalUrl = shareUserAccount?.avatar_large{
           imgView.sd_setImageWithURL(NSURL(string: avatalUrl), placeholderImage: UIImage(named: "avatar_default_big"))
        }
    }
    
    override func viewDidAppear(animated: Bool) {
        
        startAnima();
        
        
        
    }
    // 开始动画
    func startAnima(){
       // self.imageBottomConstrain?.constant = -(self.view.frame.height - 160);
         imageBottomConstrain?.constant = -(UIScreen.mainScreen().bounds.height - 200)
       UIView .animateWithDuration(2, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 5, options: UIViewAnimationOptions.CurveEaseIn, animations: { () -> Void in
        self.view.layoutIfNeeded();
        
        }) { (_) -> Void in
         
             UIView.animateWithDuration(1.0, animations: { () -> Void in
                self.lab.alpha = 1;
                }, completion: { (_ ) -> Void in
            UIApplication.sharedApplication().keyWindow?.rootViewController = myTabBarVC();
             })
        }
    
    }
    func creatUI(){
        
        view.addSubview(backImage);
        view.addSubview(imgView);
        view.addSubview(lab);
        
        imgView.translatesAutoresizingMaskIntoConstraints = false;
        
        imageBottomConstrain = NSLayoutConstraint(item: imgView, attribute: NSLayoutAttribute.Bottom, relatedBy: .Equal, toItem: self.view, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: -160);
        
        view.addConstraint(imageBottomConstrain!);
        
        backImage.translatesAutoresizingMaskIntoConstraints = false;
        
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[sub]|", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views:["sub" : backImage]))
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[sub]|", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil , views: ["sub" : backImage]));
        
        
        view.addConstraint(NSLayoutConstraint(item: imgView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0));
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[sub(90)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil , views: ["sub" : imgView]));
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[sub(90)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil , views: ["sub": imgView]));
        
        lab.translatesAutoresizingMaskIntoConstraints = false;
        view.addConstraint(NSLayoutConstraint(item: lab, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: imgView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0));
        view.addConstraint(NSLayoutConstraint(item: lab, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: imgView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 8));
        
    }
    
    
    // MARK: 懒加载创建控件
    lazy var backImage: UIImageView = {
    
     return UIImageView(image: UIImage(named: "ad_background"))
    }()
    
    lazy var imgView: UIImageView = {
        
        let imgView = UIImageView();
        imgView.layer.cornerRadius = 45;
        
        
        return imgView;
    }()
   
    lazy var lab: UILabel = {
    
        let lab = UILabel();
        lab.text = "欢迎回来";
        lab.font =  UIFont.systemFontOfSize(18);
        lab.textColor =  UIColor.blackColor();
        lab.alpha = 0;
        return lab;
    }()
    
}
